<?php 
class test extends IController
{
	//添加
	public function add()
	{
		$this->redirect('add');
	}
	//进行添加
	public function add_do()
	{
		//获取值
		$name =IReq::get('names');
		// print_r($name);die;
		$pwd = IReq::get('pwd');
		// print_r($pwd);
		//放到数组
		$arr = array('name'=>$name,'pwd'=>$pwd);
		//实例化
		$model = new IModel('users');
		$model->setdata($arr);
		//添加
		$res = $model->add();
		//判断添加
		if($res)
		{
			echo "ok";
			$this->redirect('show');
		}
		else
		{
			echo "no";
		}
	}
	//展示
	public function show()
	{
		//实例化
		$model = new IModel("users");
		$this->data = $model->query();
		// print_r($data);
		//渲染
		$this->redirect('show');
	}
	//删除
	public function del()
	{
		//接id
		$id = IReq::get('id');
		// print_r($id);
		//实例化
		$model = new IModel('users');
		$res = $model->del("id=$id");
		if($res)
		{
			echo "ok";
			$this->redirect('show');
		}
		else
		{
			echo "no";
		}
	}
	//修改
	public function upd()
	{
		//接id
		$id = IReq::get('id');
		//实例化
		$model = new IModel('users');
		$this->data = $model->query("id=$id");
		// $this->setRenderData($data);
		$this->redirect('upd');
	}
	//进行修改
	public function upd_do()
	{
		//接Id 修改的内容
		$id =IReq::get('id');
		// print_r($id);die;
		$name = IReq::get('names');
		$pwd = IReq::get('pwd');
		$arr = array(
			'name'=>$name,
			'pwd'=>$pwd
		);
		$model = new IModel('users');
		$model->setdata($arr);
		$res = $model->update("id=$id");
		if($res)
		{
			echo "ok";
			$this->redirect('show');
		}
		else
		{
			echo "no";
		}
	}
	public function maptest()
	{
		$this->redirect('maptest');
	}
	public function map_do()
	{
		// echo 1;die;
		$address = IReq::get("address");
		// print_r($address);die;
		$ak = "KDjHQYgPuL1ppkGwiUjQOzrIuF9ZQ6rC";
		$url = "http://api.map.baidu.com/geocoder/v2/?address=$address&output=json&ak=$ak";
		$data = file_get_contents($url);
		$res = json_decode($data,true);
		// echo "<pre>";
		// print_r($res);die;
		$ress = array('lng'=>$res['result']['location']['lng'],'lat'=>$res['result']['location']['lat']);
		// print_r($ress);die;
		$ress = json_encode($ress);
		echo $ress;
	}
	public function jiu()
	{
		$this->redirect('jiu');
	}
	public function map()
	{
		$address = IReq::get("address");
		// print_r($address);die;
		$ak = "i556Grbb9ZZkDaSqweNWGrmD3siMvdqD";
		$url = "http://api.map.baidu.com/geocoder/v2/?address=$address&output=json&ak=$ak";
		// print_r($url);die;
		$data = file_get_contents($url);
		$res  = json_decode($data,true);
		// print_r($res);die;
		$ress = array('lng'=>$res['result']['location']['lng'],'lat'=>$res['result']['location']['lat']);
		// print_r($ress);die;
		$model = new IModel('ip');
		$model->setdata($ress);
		$resss = $model->add();
		if($resss)
		{
			$ress = json_encode($ress);
			echo $ress;
		}
		
	}
	public function wei()
	{
		$this->redirect("wei");
	}
	public function wei_do()
	{
		$address = IReq::get("address");
		$ak = "nWI7ljhWyf4f4Nzi1G20pX2743KWrG2F";
		$url = "http://api.map.baidu.com/geocoder/v2/?address=$address&output=json&ak=$ak";
		$data = file_get_contents($url);
		// print_r($data);
		$datas = json_decode($data,true);
		// echo "<pre>";
		// print_r($datas);
		$res = array('lng'=>$datas['result']['location']['lng'],'lat'=>$datas['result']['location']['lat']);
		$ress = json_encode($res);
		echo $ress;
	}
	public function qq_login()
	{	
		$appid='101353491';			// APPID
		$appkey='df4e46ba7da52f787c6e3336d30526e4';			// APPKEY
		$redirect_uri='http://www.iwebshop.com/index.php';		// 回调地址
		$state = rand(1000,9999);
		$url = "https://graph.qq.com/oauth2.0/authorize?response_type=code&client_id=$appid&redirect_uri=$redirect_uri&state=$state";
		header("location:$url");
	}
	// public function qq()
	// {
	// 	$appid='101353491';			// APPID
	// 	$appkey='df4e46ba7da52f787c6e3336d30526e4';			// APPKEY
	// 	$redirect_uri='http://www.iwebshop.com/index.php';		// 回调地址
	// 	$statu = rand(1000,9999);
	// 	$url = "https://graph.qq.com/oauth2.0/authorize?response_type=code&client_id=$appid&redirect_uri=$redirect_uri&state=$statu";
	// 	header("location:$url");
	// }
	public function bigfile()
	{
		// print_r($_FILES);die;
		$file = $_FILES['sliceFile'];
		$tmp_name = $file['tmp_name'];
		$sliceNo = $_POST['sliceNo'];
		$filename = $_POST['filename'];
		$blob_count = $_POST['blob_count'];
		$this->movesliceFile($tmp_name,$sliceNo,$filename);
		if ($blob_count == $sliceNo) {
			$this->msgsliceFile($sliceNo,$filename);
			$this->deletesliceFile($sliceNo,$filename);
		}
	}
	public function movesliceFile($tmp_name,$sliceNo,$filename)
	{
		$destination = "upload/video/".$filename."-".$sliceNo;
		move_uploaded_file($tmp_name, $destination);
	}
	public function msgsliceFile($sliceNo,$filename)
	{
		$data = '';
		for ($i=1; $i <=$sliceNo; $i++) { 
			$path = "upload/video/".$filename."-".$i;
			$data.= file_get_contents($path);
		}
		$new_path = "upload/video/".$filename;
		file_put_contents($new_path, $data);
	}
	public function deletesliceFile($sliceNo,$filename)
	{
		for ($i=1; $i <=$sliceNo ; $i++) { 
			$path = "upload/video/".$filename."-".$i;
			unlink($path); 
		}
	}
	public function files()
	{
		$file = $_FILES['sliceFile'];
		// print_r($file);
		$tmp_name = $file['tmp_name'];
		$sliceNo = $_POST['sliceNo'];
		$filename = $_POST['filename'];
		$blob_count = $_POST['blob_count'];
		$this->movessliceFile($tmp_name,$sliceNo,$filename);
		if ($blob_count == $sliceNo) {
			$this->chengsliceFile($sliceNo,$filename);
			$this->delsliceFile($sliceNo,$filename);
		}
	}
	public function movessliceFile($tmp_name,$sliceNo,$filename)
	{
		$destination = "upload/video/".$filename."_".$sliceNo;
		move_uploaded_file($tmp_name, $destination);
	}
	public function chengsliceFile($sliceNo,$filename)
	{
		$data = '';
		for ($i=1; $i <=$sliceNo ; $i++) { 
			$path = "upload/video/".$filename."_".$i;
			$data.=file_get_contents($path);
		}
		$new_path = "upload/video/".$filename;
		file_put_contents($new_path, $data);
	}
	public function delsliceFile($sliceNo,$filename)
	{
		for ($i=1; $i <=$sliceNo; $i++) { 
			$path = "upload/video/".$filename."_".$i;
			unlink($path);
		}
	}
	public function Filess()
	{
		$file = $_FILES['sliceFile'];
		$sliceNo = $_POST['sliceNo'];
		$times  =$_POST['times'];
	}
	public function qqlogin()
	{
		$appid='101353491';			// APPID
		$appkey='df4e46ba7da52f787c6e3336d30526e4';			// APPKEY
		$state = rand(1111,9999);
		$redirect_uri='http://www.iwebshop.com/index.php';		// 回调地址
		$url = "https://graph.qq.com/oauth2.0/authorize?response_type=code&client_id=$appid&redirect_uri=$redirect_uri&state=$state";
		header("location:$url");
	}
}
?>